# -*- coding: utf-8 -*-

from qiniu import Auth, put_file, etag

# 需要填写你的 Access Key 和 Secret Key
access_key = 'f3jP9-aCVZWG2QgdxAwL_2qIXZaSdbjTUsfAa5Th'
secret_key = '99jjCCFpfjenCfswaeBm_7833P5vwxx8d7eej9OI'
# 要上传的空间
bucket_name = 'pay-image'


class QiniuClient:
    def __init__(self, access_key, secret_key, bucket_name):
        self.access_key = access_key
        self.secret_key = secret_key
        self.bucket_name = bucket_name
        print bucket_name
        self.q = Auth(access_key, secret_key)

    def get_token(self, key, timeout=120):
        """key 上传到七牛后保存的文件名"""
        return self.q.upload_token(self.bucket_name, key, timeout)

    def upload_file(self, token, key, localfile):
        ret, info = put_file(token, key, localfile)
        print(info)
        print ret


# # 要上传文件的本地路径
# localfile = './sync/bbb.jpg'
#
# ret, info = put_file(token, key, localfile)
# print(info)
# assert ret['key'] == key
# assert ret['hash'] == etag(localfile)

client = QiniuClient(access_key, secret_key, bucket_name)


def get_qiniu_token(key):
    return client.get_token(key)


# http://image.163cilian.com/alipay_qr01.png
if __name__ == '__main__':
    localfile = '/Users/kael/Downloads/alipay_qr.png'
    key = 'alipay_qr01.png'
    token = client.get_token(key)
    print token
    client.upload_file(token, key, localfile)
